﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Timers;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;


namespace LP2
{
    class CreaturesList
    {   
        public List<Enemy> enemys;
        public List<Ally> allys;
        Shooter s;
        CreaturePropertyConfig cpc;
        System.Timers.Timer timer;
        int pos;
        Boolean flgTimerStart;
        Boolean flgTimerReady;

        public CreaturesList(Shooter s,CreaturePropertyConfig cpc)
        {
            enemys=new List<Enemy>();
            allys=new List<Ally>();
            this.s = s;
            this.cpc = cpc;
            flgTimerReady = true;
        }


        public void RemoveAlly(int pos)
        {
            this.pos = pos;

            timer = new System.Timers.Timer();
            timer.Interval = 2000;
            timer.Elapsed += new ElapsedEventHandler(RemoveAllyByTimer);
            timer.Start();
        }

        private void RemoveAllyByTimer(object sender, EventArgs e)
        {   
            if (allys.Count > 1)
            {
                Console.WriteLine(s.getPosition());
                allys[pos + 1].setPosition(s.getPosition());
                allys[pos + 1].setInitPosition(s.getPosition());
            }
            allys.RemoveAt(pos);
            timer.Stop();
            timer.Close();
            timer.Dispose();

        }

        public void RemoveEnemy(Enemy e)
        {   
            enemys.Remove(e);
        }

        public void addEnemy(Enemy e)
        {
            enemys.Add(e);

        }

        public void addAlly(Ally a)
        {
            allys.Add(a);

        }

        public List<Enemy> getListEnemys()
        {
            return this.enemys;
        }
        
        public List<Ally> getListAllys()
        {
            return this.allys;
        }

        public void Draw(SpriteBatch spBatch)
        {
            spBatch.Begin();
            
                foreach (Ally a in allys)
                {
                    a.Draw(spBatch);
                }

                foreach (Enemy a in enemys)
                {
                    a.Draw(spBatch);
                }
            

        }

        public void LoadContent(ContentManager content)
        {

            foreach (Ally a in allys)
            {
                a.LoadContent(content);
            }

            foreach (Enemy e in enemys)
            {
                e.LoadContent(content);
            }


        }

        public void loadCreatures(String valueXML)
        {   
            String[] values;
            int x, y;


                values = valueXML.Split('/');
                char type = Convert.ToChar(values[0]);

                int typeCreature = Convert.ToInt32(values[1]);
                

                if (type == 'A')
                {
                    Ally nAlly = new Ally(cpc,typeCreature,s,this.allys);
                    allys.Add(nAlly);

                }
                else
                {
                    x = Convert.ToInt32(values[2]);
                    y = Convert.ToInt32(values[3]);
                    Enemy nEnemy = new Enemy(cpc,typeCreature,x,y);
                    enemys.Add(nEnemy);

                }

        }
    }
}
